/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/9
 * Time: 15:29
 * Description:
 */
package com.hangkai.reggie.exception;

import com.hangkai.reggie.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {
    /**
     * 异常处理方法
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){

        // TODO PS : 个人发现这里的员工添加页面可以直接访问到 http://localhost/backend/page/member/add.html        // 故加一个拦截器

        log.info(ex.getMessage());
        if(ex.getMessage().contains("Duplicate entry")){
            String[] strs = ex.getMessage().split(" ");
            String msg = strs[2] + "已存在";
            return Result.error(msg);
        }
        return Result.error("未知错误");
    }
    @ExceptionHandler(CustomException.class)
    public Result<String> CustomException(CustomException ex){
        log.info(ex.getMessage());
        return Result.error(ex.getMessage());
    }
//    @ExceptionHandler(Exception.class)
//    public Result<String> AllexceptionHandler(Exception ex){
//        return Result.error("未知错误(all)");
//    }

}
